package com.example.demo.controller.admin;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.CourseStudentMap;
import com.example.demo.mapper.StudentMap;
import com.example.demo.po.dto.StudentParam;
import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sc")
public class StudentCourseController {

    @Autowired
    CourseStudentMap courseStudentMap;
    @Autowired
    StudentMap studentMap;

    @GetMapping("/get")
    public Result GetCourses(@RequestParam("id") int id){


        StudentParam studentParam = studentMap.GetStudentByUserId(id);

        return ResultUtil.success(courseStudentMap.GetCourses(studentParam.getId()));
    }

    @GetMapping("/delete")//courseid="+id+"&userid
    public Result DeleteCourses(@RequestParam("id") int id ){

        courseStudentMap.DeleteCourse(id);
        return ResultUtil.success("删除成功");
    }



    @GetMapping("/Search")
    public Result SearchCourse(@RequestParam("id") int id,@RequestParam("coursename")String courseName,@RequestParam("teachername")String teacherName){
        System.out.println(id);
        StudentParam studentParam = studentMap.GetStudentByUserId(id);

        if(StringUtils.isEmpty(courseName)&&StringUtils.isEmpty(teacherName)){
            return ResultUtil.success(      courseStudentMap.GetCourses(studentParam.getId()) );

        }

        if(!StringUtils.isEmpty(courseName)&& !StringUtils.isEmpty(teacherName)){
            return   ResultUtil.success( courseStudentMap.GetCourseByAll(studentParam.getId(),"%"+courseName+"%","%"+teacherName+"%")) ;
        }else if(!StringUtils.isEmpty(courseName)){
            return ResultUtil.success( courseStudentMap.GetCourseByName(studentParam.getId(),"%"+courseName+"%")) ;
        }else {
            return ResultUtil.success( courseStudentMap.GetCourseByTeacherName(studentParam.getId(),"%"+teacherName+"%")) ;
        }



    }



    @GetMapping("/getStuddent")
    public Result GetStuddent(@RequestParam("id") int courseid){
        return ResultUtil.success(courseStudentMap.GetStudent(courseid));
    }

    @GetMapping("/deleteCourse")
    public Result DeleteMyCourse(@RequestParam("courseid") int courseid,@RequestParam("stuid") int studentid){

        courseStudentMap.DeleteMyCourse(courseid,studentid);
        return ResultUtil.success("删除成功");
    }


}
